/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.UserHome;
import com.iti.jets.gpts.pojo.User;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author YASMIN-ITI
 */

@Path("/authontication")
public class AuthonticationService {
    
     SessionFactory sessionFactory = new Configuration().configure("com/iti/jets/gpts/config/hibernate.cfg.xml").buildSessionFactory();
    UserHome userHome = new UserHome(sessionFactory);
    
    @Path("/login")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
 
     public String login(@QueryParam(value="email") String email,@QueryParam(value = "pass") String pass)
     {
         try{
            
            User userr = userHome.getUser(email, pass);
            
            System.out.println("Ana f el login Server");
            if(userr == null)
                return "no";
            else
            {
                JSONObject userJsonObject = new JSONObject();
                
                userJsonObject.put("email", userr.getEmail());
                userJsonObject.put("password", userr.getPassword());
                userJsonObject.put("id", userr.getId());
                userJsonObject.put("isAdmin", userr.getIsAdmin());
                return userJsonObject.toString();
            }
         }
         catch(Exception ex)
         {
             ex.printStackTrace();
             System.out.println("Error in login Server");
         }
         return "Done";
     }
     
     @Path("/register")
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     
     public String insertUser(@QueryParam(value="email") String email
             ,@QueryParam(value = "pass")String pass 
             ,@QueryParam(value = "id") String id)
     {
         System.out.println("Ana f el register Server");
        
         try
         {
             
             int iD = Integer.valueOf(id);
            
             User user = userHome.getUserEmail(email);
             if(user == null)
             {
                 User userData = new User();
                 userData.setEmail(email);
                 userData.setId(iD);
                 userData.setPassword(pass);
                 userData.setIsAdmin(Boolean.FALSE);
                 userHome.create(userData);
                 return "true";
                 
             }
             else
             {
                 System.out.println("User already exist");
                 return "false";
             }
         }
         catch(Exception ex)
         {
             ex.printStackTrace();
             System.out.println("Error in server Register");
             return "false";
         }
         
     }
}
